package locations.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import locations.model.TipoPractica;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import locations.dao.implement.PracticeTypeDAOHibernateJPA;

public class SavePracticeTypeAction extends ActionSupport 
        implements ModelDriven<TipoPractica>, ServletRequestAware, ServletResponseAware
{

    private static final long serialVersionUID = -8124967730231198191L;
	
    private HttpServletRequest request; 
    private HttpServletResponse response; 
    private TipoPractica practiceType = new TipoPractica();

    public String execute()
    {
        PracticeTypeDAOHibernateJPA dao = new PracticeTypeDAOHibernateJPA();
        dao.save(this.practiceType);
        return SUCCESS;
    }

    public TipoPractica getPracticeType()
    {
        return practiceType;
    }

    public void setPracticeType(TipoPractica practiceType)
    {
        this.practiceType = practiceType;
    }

    @Override
    public TipoPractica getModel()
    {
        return this.practiceType;
    }

    public HttpServletRequest getRequest()
    {
        return request;
    }

    public void setRequest(HttpServletRequest request)
    {
        this.request = request;
    }

    public void setResponse(HttpServletResponse response)
    {
        this.response = response;
    }

    public HttpServletResponse getResponse()
    {
        return this.response;
    }

    @Override
    public void setServletResponse(HttpServletResponse response)
    {
        this.response = response;
    }

    @Override
    public void setServletRequest(HttpServletRequest request)
    {
        this.request = request;
    }
    
    public void validate()
    {
        boolean ok = true;
        String code = this.getPracticeType().getCodigo();
        String description = this.getPracticeType().getDescripcion();
        String duration = this.getPracticeType().getDuracion();
        if (code.equals(""))
        {
            addFieldError("codigo", "El codigo no puede estar en blanco");
                ok = false;
        }
        if (description.equals(""))
        {
            addFieldError("descripcion", "La descripcion no puede estar en blanco");
                ok = false;
        }
        if (duration.equals(""))
        {
            addFieldError("duracion", "La duracion no puede estar en blanco");
                ok = false;
        }
        if (ok)
        {
            PracticeTypeDAOHibernateJPA dao = new PracticeTypeDAOHibernateJPA();
            long auxId = (this.getPracticeType().getId() == null? 0 : this.getPracticeType().getId());
            if (dao.exists(code, auxId))
            {
                addFieldError("codigo", "Ya existe un tipo de practica con ese codigo");
                ok = false;
            }
        }
    }
    
}
